package cn.Y_Qiu.pet.controller;

import cn.Y_Qiu.basic.util.Ajaxresult;
import cn.Y_Qiu.basic.util.PageList;
import cn.Y_Qiu.pet.domain.Pet;
import cn.Y_Qiu.pet.domain.SearchMasterMsg;
import cn.Y_Qiu.pet.query.SearchMasterMsgQuery;
import cn.Y_Qiu.pet.service.ISearchMasterMsgService;
import cn.Y_Qiu.pet.util.LoginContext;
import cn.Y_Qiu.user.domain.LoginInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;

    @GetMapping
    public List<SearchMasterMsg> getAll(){
        return seachMasterMsgService.listAll();
    }

    @PostMapping("/publish")
    public Ajaxresult seachMsgPublish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            LoginInfo loginInfo = LoginContext.getLoginInfo(request);
            seachMasterMsgService.searchpublish(searchMasterMsg, loginInfo.getId());
            return Ajaxresult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return Ajaxresult.me().setMessage("发布失败");
        }
    }

    // 查询待处理消息
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            PageList<SearchMasterMsg> pending = seachMasterMsgService.pending(query, LoginContext.getLoginInfo(request).getId());
            return pending;
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }

    }

    //查询已处理消息
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            return seachMasterMsgService.processed(query, LoginContext.getLoginInfo(request).getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }

    }


    //个人中心，用户自己的消息
    @PostMapping("/user")
    public PageList<SearchMasterMsg> queryUserMsg(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            return seachMasterMsgService.queryUserMsg(query, LoginContext.getLoginInfo(request).getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<SearchMasterMsg>();
        }

    }

    @PutMapping("/handle")
    public Ajaxresult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            return seachMasterMsgService.handle(pet, LoginContext.getLoginInfo(request).getId());
        } catch (Exception e) {
            e.printStackTrace();
            return new Ajaxresult().me().setMessage("出错");
        }
    }


}
